package com.hitplane.core
{
	import com.hitplane.elements.EnemyFly_A;
	import com.hitplane.elements.NormalUserFly;
	import com.hitplane.elements.NormalWhitBullet;
	import com.hitplane.interfaces.ICollisionAble;
	import com.hitplane.interfaces.IRecyclable;
	import com.hitplane.interfaces.IState;
	import com.hitplane.interfaces.ITick;
	import com.hitplane.manager.CheckCollisionManager;
	import com.hitplane.manager.EnterFrameManager;
	import com.hitplane.manager.ObjectPoolManager;
	
	import flash.events.Event;
	import flash.utils.getTimer;

	/**
	 * @ 描述			游戏中元素显示对象，如：飞机，子弹，导弹，等
	 * @ 作者			郑子华
	 * @ 版本			version 1.0
	 * @ 创建日期		2011-4-12上午10:05:10
	 */
	public class ItemBaseDisplay extends BaseDisplay implements ITick,ICollisionAble,IRecyclable
	{
		//是否在舞台上
		protected var isInStage:Boolean = false;
		//当前状态
		protected var state:IState;
		
		/* INTERFACE com.hitplane.interfaces.ITick */
		private var _timeGap:int;
		private var _createTime:int;
		private var _executeTime:int;
		public function get timeGap():int { return _timeGap; }
		public function set timeGap(value:int):void {_timeGap = value;}
		public function get executeTime():int { return _executeTime; }
		public function set executeTime(value:int):void {_executeTime = value;}
		public function get createTime():int { return _createTime; }
		public function set createTime(value:int):void {_createTime = value;}
		
		public function ItemBaseDisplay()
		{
			initState();
			addEvent();
		}
		//private=================================================================
		private function addToManager():void{
			CheckCollisionManager.instance.addCollisionObj(this);
			EnterFrameManager.instance.addChild(this);
		}
		//public==================================================================
		/**
		 * 设置状态 
		 * @param state
		 * 
		 */
		public function setState(state:IState):void{
			if(this.state==null){
				this.state=state;
				this.state.startState();
				return;
			}
			if(this.state==state){
				this.state.keepState();
				return;
			}
			this.state.stopState();
			this.state=state;
			this.state.startState();
		}
		//protected===============================================================
		protected function onRemoveFromStage(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			isInStage = false;
		}
		
		protected function onAddToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			isInStage = true;
			createTime = getTimer();
			addToManager();
		}
		protected function addEvent():void{
			throw new Error(this+":addEvent:子类覆盖.");
		}
		protected function removeEvent():void{
			throw new Error(this+":removeEvent:子类覆盖.");
		}
		protected function initState():void{
			throw new Error(this+":initState:子类覆盖.");
		}
		//override==================================================================
		override public function dispose():void{
			if (parent) parent.removeChild(this);
			//回收对象
			ObjectPoolManager.instance.recoveObj(this);
		}
		/**
		 * 从管理类中移除
		 * 关于该方法执行位置，本来考虑在移出场景的时候执行，想到在执行爆炸动画的时候，对象不必参与检测
		 * 因此，在何时执行该方法，需要根据具体情况来做。
		 * 如Bullet类中，在子弹发生碰撞之后removeFromManager(),在子弹超出场景范围的时候removeFromManager()
		 */
		protected function removeFromManager():void {
			CheckCollisionManager.instance.removeCollisionObj(this as ICollisionAble);
			EnterFrameManager.instance.removeChild(this as ITick);
		}
		/* INTERFACE com.hitplane.interfaces.ITick */
		public function hasCollision():void 
		{
			throw new Error(this+":hasCollision:子类覆盖.");
		}
		/**
		 * 本地设置一个时间 createTime,表示自对象参与tickTime更新的时间
		 * 在设置一个变量 timeGap,表示间隔timeGap毫秒执行updateFirTick
		 * 设置一个变量 executeTime,表示执行过的次数
		 * 通过(tickTime-createTime)/timeGap获取执行updateForTick的总次数
		 * 总次数减去已经执行过的次数，表示需要再执行的次数
		 * 
		 * 执行完之后，将总次数赋值给执行过的次数，等待下一次的tick
		 * @param	tickTime	getTimer()值
		 */
		public function updateForTick(tickTime:int):void {
			if (_createTime == 0) return;
			if (_timeGap == 0) return;
			var totalTime:int = (tickTime-_createTime) / timeGap;
			if (totalTime > _executeTime) {
				var difference:int = totalTime-_executeTime;
				while (difference) {
					if(isInStage){
						onTick();
					}
					difference--;
				}
				//如果this不在舞台上，不执行这段代码，
				//因为对象超出舞台，在的onTick方法中会将该对象回收到ObjectPool
				//onTick中将executeTime重新初始化
				if(isInStage)_executeTime = totalTime;
			}
		}
		public function onTick():void{
			throw new Error(this+":onTick:子类覆盖.");
		}
		/* INTERFACE com.hitplane.interfaces.IRecyclable */
		//重设某些信息
		public function reInit():void{
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}
		/**
		 * 准备回收
		 */
		public function readyRecycle():void{
			state.stopState();
			_timeGap=0;
			_createTime=0;
			_executeTime=0;
		}
	}
}